//
//  Block.m
//  UI__豆瓣
//
//  Created by 刘志武 on 15/12/9.
//  Copyright © 2015年 刘志武. All rights reserved.
//

#import "Block.h"
#import "AFNetworking.h"
@implementation Block




/*
 
 post 请求 parameters 填写  字典
 
 get 请求 parameters 填写nil
 
 
 */


+ (void)creatingDataTask:(NSString *)url parameters:(NSDictionary *)parameters Cookie:(NSString *)cookie User_Agent:(NSString *)user_Agent successAFNBlock:(AFNBlock)block {
    
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    manager.securityPolicy.allowInvalidCertificates = NO;
    
    
    if (parameters) {
        
        NSURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:url parameters:parameters error:nil];
        
        
        NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            if (error) {
                NSLog(@"Error: %@", error);
            } else {
                
                block (responseObject);
            }
        }];
        [dataTask resume];
        
        
    }
    else {
        
        NSMutableURLRequest *request =  [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:url parameters:parameters error:nil];
        
        [request setValue:cookie forHTTPHeaderField:@"Cookie"];
        
        [request setValue:user_Agent forHTTPHeaderField:@"User-Agent"];
        
        NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            if (error) {
                NSLog(@"Error: %@", error);
            } else {
                
                block (responseObject);
            }
        }];
        [dataTask resume];
        
        
    }
    
    
}




@end
